/************************************************************************

								Heroes IV
					  Copyright 2000, The 3DO Company

	------------------------------------------------------------------
  						       help_balloon.h

	$Header: /game/help_balloon.h $

	$NoKeywords: $

 ************************************************************************/
// kill the 255+ character identifier warning.  Templates tend to exceed that easily.
#include "precompiled.h"
#pragma warning( disable: 4786 )

#include "help_balloon.h"

#include "convert_16_bit.h"
#include "font.h"
#include "paint_surface.h"
#include "standard_fonts.h"

// -------------------------------------------------------
// text box with automatic, opaque background
// -------------------------------------------------------
t_help_balloon::t_help_balloon( t_screen_point point, char const* text )
			  : t_text_window( ::get_font( 17 ), 0, text, t_pixel_24(0,0,0) )
{
	m_has_transparency = k_no_transparency;
	m_drop_shadow = false;
	m_justification = k_justify_center;
	m_center_vertical = true;

	t_screen_rect rect;
	t_screen_rect frame_rect = get_main_window()->get_client_rect();

	m_text_width = get_parent()->get_width() - 8;
	m_text_width = m_font->wrap_text( m_text_block, text, m_text_width );
	rect.top = 0;
	rect.left = 0;
	rect.right = m_text_width + 8;
	rect.bottom = m_text_block.size() * m_font->get_height() + 8;

	if ( rect.height() > point.y )
		rect = rect + point + t_screen_point( 0, 20 );
	else
		rect = rect + point - t_screen_point( 0, rect.bottom );

	if (rect.right > frame_rect.right)
		rect = rect + t_screen_point( frame_rect.right - rect.right, 0 );
	init( rect );
}

// -------------------------------------------------------
// draw a horizontal line
// -------------------------------------------------------
static void draw_horizontal( t_abstract_bitmap16& bitmap, t_screen_point point, int end_x,
							 t_uint16 color )
{
	t_screen_rect rect = bitmap.get_rect();

	if (point.y < rect.top)
		return;
	if (point.y >= rect.bottom)
		return;

	if (point.x < rect.left )
		point.x = rect.left;

	if (end_x >= rect.right)
		end_x = rect.right;

	if (end_x <= point.x)
		return;

	int       width = end_x - point.x;
	t_uint16* ptr = bitmap.advance_line( bitmap.get_data_ptr() + point.x, point.y );

	while (width--)
		*ptr++ = color;
}

// -------------------------------------------------------
// draw a vertical line
// -------------------------------------------------------
static void draw_vertical( t_abstract_bitmap16& bitmap, t_screen_point point, int end_y,
						   t_uint16 color )
{
	t_screen_rect rect = bitmap.get_rect();

	if (point.x < rect.left)
		return;
	if (point.y >= rect.right)
		return;

	if (point.y < rect.top)
		point.y = rect.top;
	if (end_y >= rect.bottom)
		end_y = rect.bottom;

	if (end_y <= point.y)
		return;

	int height = end_y - point.y;
	t_uint16* ptr = bitmap.advance_line( bitmap.get_data_ptr() + point.x, point.y );

	while (height--)
	{
		*ptr = color;
		ptr = bitmap.advance_line( ptr );
	}
}

// -------------------------------------------------------
// text box with automatic, opaque background
// -------------------------------------------------------
void t_help_balloon::paint( t_paint_surface& paint_surface )
{
	{
		t_screen_rect           client_rect = get_client_rect();
		t_screen_rect           paint_rect = paint_surface.get_rect();
		t_abstract_bitmap16_ptr bitmap = paint_surface.get_bitmap();

		client_rect -= paint_rect.top_left();

		// draw black lines around the edge
		draw_horizontal( *bitmap, client_rect.top_left(), client_rect.right, 0 );
		draw_vertical( *bitmap,   client_rect.top_left(), client_rect.bottom, 0 );
		draw_horizontal( *bitmap, client_rect.bottom_left() - t_screen_point(0,1), client_rect.right, 0 );
		draw_vertical( *bitmap,   client_rect.top_right() - t_screen_point(1,0),   client_rect.bottom, 0 );

		client_rect.left++;
		client_rect.top++;
		client_rect.right--;
		client_rect.bottom--;

		// fill center with off-white.
		client_rect = intersection( client_rect, bitmap->get_rect() );
		if (client_rect.width() > 0 && client_rect.height() > 0)
			fill( *bitmap, client_rect, convert_to_16_bit( t_pixel_24( 255, 255, 216 ) ) );
	}
	// must destroy the bitmap pointer before we can call the text_window paint.
	t_text_window::paint( paint_surface );
}
